//
//  OMTAlbumCell.m
//  OMTImagePicker
//
//  Created by yizhaorong on 2017/11/16.
//

#import "OMTAlbumCell.h"
#import "OMTImagePickerController.h"

@interface OMTAlbumCell ()
// 封面
@property (nonatomic, strong) UIImageView *coverView;
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 数量
@property (nonatomic, strong) UILabel *countLabel;
// 选中状态图标
@property (nonatomic, strong) UIImageView *selectedIconView;

@end

@implementation OMTAlbumCell

#pragma mark - Lifecycle
- (void)dealloc {
    // [self unregisterNotifications]
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
        [self setLayout];
    }
    return self;
}

#pragma mark - View Lifecycle
- (void)setupSubviews {
    // 初始化子视图
    _coverView = [UIImageView new];
    _coverView.contentMode = UIViewContentModeScaleAspectFill;
    _coverView.clipsToBounds = YES;
    [self.contentView addSubview:_coverView];
    
    _titleLabel = [UILabel new];
    [self.contentView addSubview:_titleLabel];
    
    _countLabel = [UILabel new];
    _countLabel.font = [UIFont systemFontOfSize:12];
    _countLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_countLabel];
    
    _selectedIconView = [UIImageView new];
    [self.contentView addSubview:_selectedIconView];
    
    if (omt_album_is_rtl_language()) {
        CGAffineTransform transform = CGAffineTransformMakeScale(-1, 1);
        self.contentView.transform = transform;
        _coverView.transform = transform;
        _titleLabel.transform = transform;
        _countLabel.transform = transform;
        _selectedIconView.transform = transform;
    }
}

- (void)setLayout {
    // 设置或更新布局
    CGFloat padding = 10;
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    
    CGFloat coverWH = 54;
    CGFloat coverX = padding;
    CGFloat coverY = (height - coverWH) / 2;
    CGRect coverFrame = CGRectMake(coverX, coverY, coverWH, coverWH);
    self.coverView.frame = coverFrame;
    
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat titleH = titleSize.height;
    CGFloat titleW = titleSize.width;
    CGFloat titleY = 5;
    CGFloat titleX = CGRectGetMaxX(coverFrame) + padding;
    CGRect titleFrame = CGRectMake(titleX, titleY, titleW, titleH);
    self.titleLabel.frame = titleFrame;
    
    CGSize countSize = [self.countLabel sizeThatFits:CGSizeMake(width, height)];
    CGFloat countH = countSize.height;
    CGFloat countW = countSize.width;
    CGFloat countY = CGRectGetMaxY(titleFrame) + 5;
    CGFloat countX = CGRectGetMaxX(coverFrame) + padding;
    CGRect countFrame = CGRectMake(countX, countY, countW, countH);
    self.countLabel.frame = countFrame;
    
    CGSize checkedSize = [self.selectedIconView sizeThatFits:CGSizeMake(width, height)];
    CGFloat checkedH = checkedSize.height;
    CGFloat checkedW = checkedSize.width;
    CGFloat checkedY = (height - checkedH) / 2;
    CGFloat checkedX = width - checkedW - 16;
    CGRect checkedFrame = CGRectMake(checkedX, checkedY, checkedW, checkedH);
    self.selectedIconView.frame = checkedFrame;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setLayout];
}

#pragma mark - Notifications
/*
 - (void)registerNotifications {
 // 注册通知
 }
 
 - (void)unregisterNotifications {
 // 注销通知
 }
 */
#pragma mark - Actions

#pragma mark - Private

#pragma mark - Public


#pragma mark - Protocol conformance


#pragma mark - Lazyload


#pragma mark - Custom Accessors
//自定义 setter getter
- (void)setAlbum:(OMTAlbum *)album {
    _album = album;
    OMTAlbumManager *albumManager = album.albumManager;
    UIImage *(^AlbumImageNamed)(NSString *) = [albumManager.builder.imageWithNamed copy];
    _selectedIconView.image = AlbumImageNamed(@"omt_album_checked");
    __weak typeof(self) weakSelf = self;
    [albumManager coverWithAlbum:album completion:^(UIImage *image, OMTAlbum *album, BOOL finished) {
        typeof(weakSelf) strongSelf = weakSelf;
        if (!strongSelf) return;
        
        if (!finished) return;
        if (strongSelf.album != album) return;
        strongSelf.coverView.image = image;
    }];
    self.titleLabel.text = album.name;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", album.count];
    self.selectedIconView.hidden = !album.selected;
    [self setLayout];
}

@end
